import { Role } from '@app/modules';
import { AuditBase } from '@app/types/entities';
import { ApiProperty } from '@nestjs/swagger';
import { Column, Entity, ManyToMany, ManyToOne, OneToMany, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Permission extends AuditBase {
  @PrimaryGeneratedColumn()
  id: number;

  @ApiProperty({ description: '权限名称' })
  @Column()
  name: string;

  @ApiProperty({ description: '可操作的资源' })
  @Column()
  resource: string;

  @ApiProperty({ description: '资源的可操作性' })
  @Column()
  action: string;

  @ApiProperty({ type: () => Permission })
  @ManyToOne(() => Permission, (permission) => permission.children)
  parent?: Permission;

  @ApiProperty({ type: () => [Permission] })
  @OneToMany(() => Permission, (permission) => permission.parent)
  children?: Permission[];

  @ManyToMany(() => Role, (role) => role.permisions)
  roles: Role[];
}
